package com.xyht.medicine_management_system.modules.payment.alipay.util;

import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ExceptionCast;

import java.math.BigDecimal;

public class ToolUtil {

    /**
     * @description 除法运算
     * @param v1 被除数
     * @param v2 除数
     * @param scale 精确到小数后几位 大于0
     *
     * @create 2023/12/28
     * @author dxn
    */
    public static String numDiv(double v1, double v2, int scale){
        if (scale < 0){
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        BigDecimal result = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
        return result.toString();
    }
}
